/**
 * @author caojj
 * @version 	2.0.1.6
 * @sdoc + 增加, - 去除, # 修复, * 计划
 * @plan
 * 		* 改进继承机制
 * @history
 * 		1.0.1.1		+ 页面离开时清理内存 @20080412
 * 		1.0.2.1		# 单独引用文件时没有定义FWK空间的错误 @20080412
 * 		2.0.0.0		+ 改进加载机制
 * 					# 规范命名
 * 					+ 文档
 * 					+ $ $C $T 的常用方法
 *		2.0.1.0		+ 防止重复加载 @20080906
 *					+ 自动载入核心包 @20080906
 */

FWK={};		//命名空间
FWK.libPath = "";
FWK.libLoaded = [];
FWK.getLibPath = function() {
	var ret = "";

	var scripts = document.getElementsByTagName("script");
	for (var i=0; i<scripts.length; i++) {
		var script = scripts[i];
		var matcher = script.src.match(/(.*)\bPackage.js\b/);
		if (script.src && matcher) {
			FWK.libPath = matcher[1];
			ret = FWK.libPath;
			break;
		}
	}
	return ret;
}
/**
 * 引用库
 * @param {Object} pkgs
 * @param {Object} path
 */
/*
 * @usage
 * 			FWK.include("FWK.Util.Ajax");
 *
 *			FWK.include([
 *				"FWK.Util.Ajax",
 *				"FWK.Misc.Misc",
 *				"FWK.Util.Dragable",
 *				"FWK.UIControl.PagerUI"
 *			]);
 */
FWK.include = function(pkgs, path) {
	function outWrite(pkg, pkgName) {
		document.write("<script language='JavaScript' type='text/javascript' src='" + pkg + "' " + (pkgName ? ("package='" +pkgName+ "'") : "") + "></script>");
	}

	function doInclude(pkg) {
		if (FWK.libLoaded.length<=0) {
			outWrite(path + "core.js", path);
			FWK.libLoaded.push("core");
		}

		if (!pkg.match(/\.js$/i)) {		//eg:FWK.Util.PagerUI
			if (FWK.libLoaded.join(",").search("\\b"+pkg+"\\b")>-1) {
				return;		//已经加载过
			} else {
				FWK.libLoaded.push(pkg);
			}

			var pkgPath = path + "/" + pkg.replace(/\./g, "/") + ".js";
			pkgPath = pkgPath.replace(/http:\/\//i, "jspath~:");
			pkgPath = pkgPath.replace(/\bFWK\b/i, "").replace(/\/+/g, "/").replace(/jspath~:/i, "http://");
			outWrite(pkgPath, pkg);
		} else {	//eg:app/common.js
			var pkgPath = pkg;
			outWrite(pkgPath);
		}
	}

	if(!path) {path=FWK.libPath?FWK.libPath:FWK.getLibPath();}
	if (pkgs instanceof Array) {
		for (var pkgIndex in pkgs) {
			var pkg = pkgs[pkgIndex];
			doInclude(pkg);
		}
	} else {
		doInclude(pkgs);
	}

	return true;
}

function $C(tag){
	return document.createElement(tag);
}

function $CT(txt){
	return document.createTextNode(txt);
}

/**
 * 确保页面完全加载成功后触发
 * 覆盖$Page_Load
 */
FWK.pageLoadFunction = function() {
	if (document.readyState == "complete" || !document.all) {
		try{$Page_Load();}catch(e){}
	} else {
		//如果没有准备好，尝试6次
		var times = 0;
		var sTimer = setInterval(function(){
			if (document.readyState == "complete" || times > 5) {
				try{$Page_Load();}catch(e){}
				clearInterval(sTimer);
			} else {
				times++;
			}
		}, 100);
	}
}

/**
 * 页面离开前触发 可以用来清理内存 @20080412
 * 重写$Page_Load
 */
FWK.pageUnloadFunction = function() {
	try{$Page_UnLoad();}catch(e){}

	if (document.expando) {
		document.expando = false;
	}

	for (var p in Function.prototype) {
		Function.prototype[p] = null;
		delete Function.prototype[p];
	}

	FWK = null;
	delete FWK;
//	delete FWK;
}
/**
 * Dom构建完成 调用
 * @param {Function} _fn
 */
FWK.bodyReady = function(_fn){
	try{
		document.addEventListener("DOMContentLoaded", _fn, false);
	}
	catch(e){
		document.attachEvent("onreadystatechange", function(){
			if (document.readyState == "complete" || document.readyState == "loaded") {
				_fn.call();
			}
		});
	}
}

/**
 * Dom筛选
 * @param {String | Fun | Element} _sizer
 * @param {String} _extend
 * _sizer String 筛选器 	打头 # id山选 *tag筛选 &name筛选
 * 						[]返回数组 {}返回对象 key以Dom的id为key
 */
function $(_sizer){
	if(typeof(_sizer)=="string"){
		var RD = document.getElementById(_sizer),RV,filter;
		if(RD){
			RV = RD;
		}
		else if(/^#(.*)\[([^:^;^@]*)\](.*)$/.test(_sizer)){
			filter = RegExp["$3"];
			var q = RegExp["$1"]==""?"" : (RegExp["$1"] + ".");
			var Rx = RegExp["$2"].split(/[ ]/g);
			RV = [];
			for(var i=0;i<Rx.length;i+=1){
				RV[i] = document.getElementById(q + Rx[i]);
			}
		}
		else if(/^#(.*)\{([^:^;^@]*)\}(.*)$/.test(_sizer)){
			filter = RegExp["$3"];
			var q = RegExp["$1"]==""?"" : (RegExp["$1"] + ".");
			var Rx = RegExp["$2"].split(/[ ]/g),d;
			RV = {};
			for(var i=0;i<Rx.length;i+=1){
				RV[Rx[i]] = document.getElementById(q + Rx[i]);
			}
		}
		else if(/^\*(.*)\[([^:^;^@]*)\](.*)$/.test(_sizer)){
			filter = RegExp["$3"];
			var fa = RegExp["$1"]?document.getElementById(RegExp["$1"]):document;
			var Rx = RegExp["$2"].split(/[ ]/g);
			RV = [];
			for(var i=0;i<Rx.length;i+=1){
				RV.concat(fa.getElementsByTagName(Rx[i]));
			}
		}
		else if(/^\*(.*)\{([^:^;^@]*)\}(.*)$/.test(_sizer)){
			filter = RegExp["$3"];
			var fa = RegExp["$1"]?document.getElementById(RegExp["$1"]):document;
			var Rx = RegExp["$2"].split(/[ ]/g),ds,j,key;
			RV = {};
			for(var i=0;i<Rx.length;i+=1){
				ds = fa.getElementsByTagName(Rx[i]);
				for(j=0;j<ds.length;j+=1){
					RV[ds[j]?(ds[j].getAttribute("id")||(i + "_" + j)):(i + "_" + j)] = ds[j];
				}
			}
		}
		else if(/^&(.*)\[([^:^;^@]*)\](.*)$/.test(_sizer)){
			filter = RegExp["$3"];
			var fa = RegExp["$1"]?document.getElementById(RegExp["$1"]):document;
			var Rx = RegExp["$2"].split(/[ ]/g);
			RV = [];
			for(var i=0;i<Rx.length;i+=1){
				RV.concat(fa.getElementsByName(Rx[i]));
			}
		}
		else if(/^&(.*)\{([^:^;^@]*)\}(.*)$/.test(_sizer)){
			filter = RegExp["$3"];
			var fa = RegExp["$1"]?document.getElementById(RegExp["$1"]):document;
			var Rx = RegExp["$2"].split(/[ ]/g),ds,j,key;
			RV = {};
			for(var i=0;i<Rx.length;i+=1){
				ds = fa.getElementsByName(Rx[i]);
				for(j=0;j<ds.length;j+=1){
					RV[ds[j]?(ds[j].getAttribute("id")||(i + "_" + j)):(i + "_" + j)] = ds[j];
				}
			}
		}
		else if(/^([^:^;^@]+)(.*)$/.test(_sizer)){
			filter = RegExp["$2"];
			var RD = document.getElementById(RegExp["$1"]);
			RV = RD;
		}
		else{
			RV = RD;
		}
	}
	else if(typeof(_sizer) == "function"){
		//附加 FWK.domReady
		FWK.bodyReady(_sizer);
		RV = null;
	}
	else{
		RV = _sizer;
	}
	if(RV && filter && /^([:;@])(.*)$/.test(filter) && FWK && FWK.Element && FWK.Elements){
		var obj = FWK.isEl(RV)?FWK.Element:FWK.Elements;
		if(RegExp["$1"]==":"){
			var fns = RegExp["$2"].replace(/^\s+|\s+$/g,"").split(/[ ]+/g);
			if(fns.length==1){
				if(obj[RegExp["$2"]]){
					obj[RegExp["$2"]].apply(RV,Array.prototype.slice.call(arguments,1));
				}
			}
			else{
				for(var i=0;i<fns.length;i+=1){
					if(obj[fns[i]]){
						obj[fns[i]].apply(RV,arguments[i+1]);
					}
				}
			}
		}
		else if(RegExp["$1"]==";"){
			FWK.extend(obj,RegExp["$2"].split(/[ ]+/g));
		}
		else if(RegExp["$1"]=="@"){
			if(obj[RegExp["$2"]]){
				RV = obj[RegExp["$2"]].apply(RV,Array.prototype.slice.call(arguments,1)) || RV;
			}
		}
	}
	return RV;
}

/**
 * 为Dom增加事件
 *
 * @param {String} _sEventType
 * @param {Function} _fnHandler
 * 例如：FWK.Element.addEvent.call(Dom,'click',function(){});
 * IE中 事件前面的on统一去除 兼容Firefox
 */
FWK.addEvent=function(_oTarget,_sEventType, _fnHandler){
	_oTarget = $(_oTarget);
	if(_oTarget.addEventListener) {
    	_oTarget.addEventListener(_sEventType, _fnHandler, false);
	}
	else if(_oTarget.attachEvent) {
  	  	_oTarget.attachEvent("on" + _sEventType, _fnHandler);
	}
	else{
    	_oTarget["on" + _sEventType] = _fnHandler;
	}
},
/**
 * 为Dom移除事件
 * @param {String} _sEventType
 * @param {Function} _fnHandler
 * 例如：FWK.Element.addEvent.call(Dom,'click',事件引用);
 * IE中 事件前面的on统一去除 兼容Firefox
 */
FWK.removeEvent=function(_oTarget,_sEventType, _fnHandler){
	_oTarget = $(_oTarget);
	if (_oTarget.removeEventListener) {
    	_oTarget.removeEventListener(_sEventType, _fnHandler, false);
	}
	else if(_oTarget.detachEvent) {
    	_oTarget.detachEvent("on" + _sEventType, _fnHandler);
	}
	else{
    	_oTarget["on" + _sEventType] = false;
	}
}

FWK.addEvent(window,"load",FWK.pageLoadFunction);
FWK.addEvent(window,"unload",FWK.pageUnloadFunction);

FWK.addEvent(window,"load",function(){
	var google = document.createElement("script");
	google.onload = google.onreadystatechange = function(){
		if(google.readyState){
			if(google.readyState == 'loaded' || google.readyState == 'complete'){
				//IE
				_uacct = "UA-4429972-1";
				try{urchinTracker();}catch(e){}
			}
		}
		else{
			//FF
			_uacct = "UA-4429972-1";
			try{urchinTracker();}catch(e){}
		}
	}
	google.setAttribute("type",'text/javascript');
	document.getElementsByTagName("head")[0].appendChild(google);
	google.setAttribute("src","http://www.google-analytics.com/urchin.js");
});